﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Ultilities.Ultility;
    using ManagerClinic.Modules;

    public partial class frmCapNhatDmTinhThanh : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler ButtonClicked = null;
        int State = 0;

        private DmTinhThanhBusiness dmTinhThanhBusiness = new DmTinhThanhBusiness();
        private DmQuocGiaBusiness dmQuocGiaBusiness = new DmQuocGiaBusiness();
        private DmTinhThanhEntity dmTinhThanhEntity = null;

        public frmCapNhatDmTinhThanh(int state)
        {
            InitializeComponent();

            LoadQuocGia();
            this.State = state;
        }

        public frmCapNhatDmTinhThanh(int state, DmTinhThanhEntity obj)
        {
            InitializeComponent();

            LoadQuocGia();
            dmTinhThanhEntity = obj;
            this.State = state;

            lkQuocGia.EditValue = dmTinhThanhEntity.MaQuocGia;
            txtMaTinhThanh.Text = dmTinhThanhEntity.MaTinhThanh;
            txtTenTinhThanh.Text = dmTinhThanhEntity.TenTinhThanh;
            txtMoTa.Text = dmTinhThanhEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmTinhThanhEntity.TrangThaiSuDung;
        }

        private void LoadQuocGia()
        {
            var lstDmQuocGiaEntity = dmQuocGiaBusiness.GetDmQuocGiaList();
            var dmQuocGiaEntity = new DmQuocGiaEntity()
            {
                MaQuocGia = ConstantManagerClinic.sValueMember,
                TenQuocGia = ConstantManagerClinic.sDisplayMember
            };
            lstDmQuocGiaEntity.Insert(0, dmQuocGiaEntity);

            lkQuocGia.Properties.DataSource = lstDmQuocGiaEntity;
            lkQuocGia.Properties.DisplayMember = "TenQuocGia";
            lkQuocGia.Properties.ValueMember = "MaQuocGia";
            lkQuocGia.EditValue = ConstantManagerClinic.sValueMember;
        }

        private void frmCapNhatDmTinhThanh_Load(object sender, EventArgs e)
        {

        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkQuocGia, "quốc gia") == false) return;
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaTinhThanh, "mã tỉnh thành") == false) return;

            dmTinhThanhEntity = new DmTinhThanhEntity()
            {
                MaTinhThanh = txtMaTinhThanh.Text,
                TenTinhThanh = txtTenTinhThanh.Text,
                MoTa = txtMoTa.Text,
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked,
                MaQuocGia = lkQuocGia.EditValue.ToString()
            };

            if (State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmTinhThanhBusiness.GetDmTinhThanh(txtMaTinhThanh.Text) != null)
                {
                    if (Validation.ValidPrimaryKey(txtMaTinhThanh, "mã tỉnh thành") == false) return;
                }

                dmTinhThanhBusiness.InsertDmTinhThanh(dmTinhThanhEntity);
                Validation.ClearControl(this);
                lkQuocGia.Focus();
            }
            else
            {
                dmTinhThanhBusiness.EditDmTinhThanh(dmTinhThanhEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (ButtonClicked != null)
                ButtonClicked(this, e);
        }

        private void frmCapNhatDmTinhThanh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
    }
}